package com.kaishengit.web;


import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AjaxServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		System.out.println("method:" + request.getMethod());
		String name = request.getParameter("name");
		
		//name = new String(name.getBytes("ISO8859-1"),"UTF-8");
		
		System.out.println("Hello! " + name);
		
		response.setContentType("text/html");
		response.addHeader("pragma", "no-cache");
		response.addHeader("cache-control", "no-cache");
		response.addHeader("expires", "0");
		
		
		PrintWriter out = response.getWriter();
		if("tom".equals(name)) {
			out.print("error");
		} else {
			out.print("ok");
		}
		
		out.flush();
		out.close();
	}
	
}
